package com.ruoyi.wx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wx.domain.Dictionary;
import com.ruoyi.wx.service.IDictionaryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 字典Controller
 *
 * @author wt
 * @date 2020-12-13
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wx/dictionary")
public class DictionaryController extends BaseController {

    private final IDictionaryService iDictionaryService;

    /**
     * 查询字典列表
     */
    @PreAuthorize("@ss.hasPermi('wx:dictionary:list')")
    @GetMapping("/list")
    public TableDataInfo list(Dictionary dictionary) {
        startPage();


        LambdaQueryWrapper<Dictionary> lqw = Wrappers.lambdaQuery(dictionary);
        if (StringUtils.isNotBlank(dictionary.getName())) {
            lqw.like(Dictionary::getName, dictionary.getName());
        }
        if (StringUtils.isNotBlank(dictionary.getKey())) {
            lqw.eq(Dictionary::getKey, dictionary.getKey());
        }
        if (StringUtils.isNotBlank(dictionary.getValue())) {
            lqw.eq(Dictionary::getValue, dictionary.getValue());
        }
        if (StringUtils.isNotBlank(dictionary.getType())) {
            lqw.eq(Dictionary::getType, dictionary.getType());
        }
        if (dictionary.getParent() != null) {
            lqw.eq(Dictionary::getParent, dictionary.getParent());
        }
        if (dictionary.getState() != null) {
            lqw.eq(Dictionary::getState, dictionary.getState());
        }
        List<Dictionary> list = iDictionaryService.list(lqw);
        return getDataTable(list);
    }


    @GetMapping("getDic")
    public  Object getDic(Dictionary dictionary){

        LambdaQueryWrapper<Dictionary> lqw = Wrappers.lambdaQuery(dictionary);
        if (StringUtils.isNotBlank(dictionary.getName())) {
            lqw.like(Dictionary::getName, dictionary.getName());
        }
        if (StringUtils.isNotBlank(dictionary.getKey())) {
            lqw.eq(Dictionary::getKey, dictionary.getKey());
        }
        if (StringUtils.isNotBlank(dictionary.getValue())) {
            lqw.eq(Dictionary::getValue, dictionary.getValue());
        }
        if (StringUtils.isNotBlank(dictionary.getType())) {
            lqw.eq(Dictionary::getType, dictionary.getType());
        }
        if (dictionary.getParent() != null) {
            lqw.eq(Dictionary::getParent, dictionary.getParent());
        }
        if (dictionary.getState() != null) {
            lqw.eq(Dictionary::getState, dictionary.getState());
        }
        return  AjaxResult.success(iDictionaryService.list(lqw));
    }

    @GetMapping(value = "/getDicBykey/{key}")
    public  Object getDicBykey(@PathVariable String key){

        Dictionary one = iDictionaryService.getOne(iDictionaryService.lambdaQuery().eq(Dictionary::getKey, key));
        return  AjaxResult.success(one);
    }





    /**
     * 导出字典列表
     */
    @PreAuthorize("@ss.hasPermi('wx:dictionary:export')")
    @Log(title = "字典", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Dictionary dictionary) {
        LambdaQueryWrapper<Dictionary> lqw = new LambdaQueryWrapper<Dictionary>(dictionary);
        List<Dictionary> list = iDictionaryService.list(lqw);
        ExcelUtil<Dictionary> util = new ExcelUtil<Dictionary>(Dictionary.class);
        return util.exportExcel(list, "dictionary");
    }

    /**
     * 获取字典详细信息
     */
    @PreAuthorize("@ss.hasPermi('wx:dictionary:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iDictionaryService.getById(id));
    }

    /**
     * 新增字典
     */
    @PreAuthorize("@ss.hasPermi('wx:dictionary:add')")
    @Log(title = "字典", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Dictionary dictionary) {
        return toAjax(iDictionaryService.save(dictionary) ? 1 : 0);
    }

    /**
     * 修改字典
     */
    @PreAuthorize("@ss.hasPermi('wx:dictionary:edit')")
    @Log(title = "字典", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Dictionary dictionary) {
        return toAjax(iDictionaryService.updateById(dictionary) ? 1 : 0);
    }

    /**
     * 删除字典
     */
    @PreAuthorize("@ss.hasPermi('wx:dictionary:remove')")
    @Log(title = "字典", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iDictionaryService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
